// download utils
var app = angular.module('app');

app.factory('downloadUtilsService', ['FileSaver', 'Blob',
    function (FileSaver, Blob) {
        var downloadUtilsService = {};

        downloadUtilsService.s2ab = function (s) {
            if (!s) {
                return;
            }
            var buf = new ArrayBuffer(s.length);
            var view = new Uint8Array(buf);
            for (var i = 0; i != s.length; ++i) view[i] = s.charCodeAt(i) & 0xFF;
            return buf;
        };

        downloadUtilsService.saveFile = function (s, fileName) {
            var blob = new Blob([downloadUtilsService.s2ab(s)], {type: ''});
            FileSaver.saveAs(blob, fileName);
        };
        return downloadUtilsService;
    }]);